package com.etop.learnapp.fragment;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.etop.learnapp.R;

/**
 * Created by John on 2015/10/26 0026.
 */
public class VideoFragment extends Fragment implements SurfaceHolder.Callback {
    MediaPlayer mediaPlayer;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    Button play, pause, stop;
    int postSize;
    private boolean flag;
    private String fileName;
    private int resID;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;
        int chapterSelect = getActivity().getIntent().getIntExtra("chapterSelect", -1);
        int sectionSelect = getActivity().getIntent().getIntExtra("sectionSelect", -1);
        int keySelect = getActivity().getIntent().getIntExtra("keySelect", -1);
        fileName="sp"+chapterSelect+"_"+sectionSelect+"_"+keySelect;
        resID=getResources().getIdentifier(fileName,"raw",getActivity().getPackageName());
        if(resID!=0)
        {
            view = inflater.inflate(R.layout.fragment_video, container, false);
            play = (Button) view.findViewById(R.id.button1);
            pause = (Button) view.findViewById(R.id.button2);
            stop = (Button) view.findViewById(R.id.button3);
            surfaceView = (SurfaceView) view.findViewById(R.id.surface);

            surfaceHolder = surfaceView.getHolder(); //SurfaceHolder是SurfaceView的控制接口
            surfaceHolder.addCallback(this);  //因为这个类实现了SurfaceHolder.Callback接口，所以回调参数直接this
            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (postSize > 0) {  //说明中途停止过（activity调用过pase方法，不是用户点击停止按钮），跳到停止时候位置开始播放

                        mediaPlayer.seekTo(postSize);   //跳到postSize大小位置处进行播放

                    }
                    else
                    {
                        System.out.println("视频位置为0");
                    }
                    mediaPlayer.start();

                }
            });
            pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer.pause();
                    //    postSize=mediaPlayer.getCurrentPosition();
                }
            });
            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer.seekTo(0);
                }
            });
        }
        else
        {
            view=inflater.inflate(R.layout.fragment_video_blank, container, false);;
        }


        return view;
    }
    class PlayMovie extends Thread {   //播放视频的线程

        int post = 0;

        public PlayMovie(int post) {
            this.post = post;

        }

        @Override
        public void run() {
            Message message = Message.obtain();
            try {
                mediaPlayer = MediaPlayer.create(getActivity(), resID);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDisplay(surfaceHolder);
                mediaPlayer.setOnPreparedListener(new Ok(post));  //设置监听事件
            } catch (Exception e) {
                message.what = 2;
                Log.e("hck", e.toString());
            }
            super.run();
        }
    }

    class Ok implements MediaPlayer.OnPreparedListener {
        int postSize;

        public Ok(int postSize) {
            this.postSize = postSize;
        }

        @Override
        public void onPrepared(MediaPlayer mp) {
//
//            if (mediaPlayer != null) {
//                mediaPlayer.start();  //开始播放视频
//                System.out.println("当前位置" + mediaPlayer.getCurrentPosition());
//                flag = true;
//            } else {
//                return;
//            }
//            if (postSize > 0) {  //说明中途停止过（activity调用过pase方法，不是用户点击停止按钮），跳到停止时候位置开始播放
//                System.out.println("seekTo++"+postSize);
//                mediaPlayer.seekTo(postSize);   //跳到postSize大小位置处进行播放
//            }

        }
    }

    @Override
    public void onResume() {
        if(mediaPlayer!= null){
            mediaPlayer.seekTo(postSize);
            mediaPlayer.start();
        }

        super.onResume();
    }

    @Override
    public void onPause() {
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {
            postSize = mediaPlayer.getCurrentPosition();
        }

        super.onPause();
    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if (postSize > 0) {    //说明，停止过activity，跳到停止位置播放
            new PlayMovie(postSize).start();
         //   postSize = 0;
        } else {
            new PlayMovie(0).start();   //表明是第一次开始播放
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        //    postSize = mediaPlayer.getCurrentPosition();
            mediaPlayer.stop();
            flag = false;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        System.gc();
    }
}
